﻿namespace EasyGo.Data.Dao.Impl
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using EasyGo.Domain.Model;

    public class CompanyDao : SqlDao, ICompanyDao
    {
        public int CreateCompany(Company company)
        {
            using (var command = this.GetStoredProcedureCommand("Company_Create"))
            {
                command.Parameters.AddWithValue("@CompanyName", company.Name);
                command.Parameters.AddWithValue("@Info", company.Description);
                return Convert.ToInt32(command.ExecuteScalar());
            }
        }

        public IEnumerable<Company> GetAllCompanies()
        {
            using (var command = this.GetStoredProcedureCommand("Company_GetAll"))
            {
                return command.ExecuteReader().ReadAll<Company>();
            }
        }

        public Company GetById(int id)
        {
            using (var command = this.GetStoredProcedureCommand("Company_GetOne"))
            {
                command.Parameters.AddWithValue("@Id", id);
                return command.ExecuteReader().ReadFirstOrDefault<Company>();
            }
        }

        public void Delete(int id)
        {
            using (var command = this.GetStoredProcedureCommand("Company_Delete"))
            {
                command.Parameters.AddWithValue("@Id", id);
                command.ExecuteNonQuery();
            }
        }

        public void UpdateCompany(Company company)
        {
            using (var command = this.GetStoredProcedureCommand("Company_Update"))
            {
                command.Parameters.AddWithValue("@CompanyName", company.Name);
                command.Parameters.AddWithValue("@Info", company.Description);
                command.Parameters.AddWithValue("@CompanyId", company.Id);
                command.ExecuteNonQuery();
            }
        }
    }
}
